package com.itheima.TCP;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

//服务器：接收到的数据写入本地文件，给出反馈
public class ServerDemo04 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(10086);
        Socket socket = null;
        while (true) {
            socket = serverSocket.accept();
            //创建网络输入流，接受发送过来的数据
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            //创建本地输出流，将接收到的数据写入本地文件
            BufferedWriter bw = new BufferedWriter(new FileWriter("day15_inter\\"+UUID.randomUUID()+".txt"));
            String line ;
            //循环写入
            while ((line = br.readLine())!=null){
                System.out.println(line);
                bw.write(line);
                bw.newLine();
            }
            bw.close();

            //接收完成，发送反馈
            OutputStream os = socket.getOutputStream();
            BufferedWriter fankui = new BufferedWriter(new OutputStreamWriter(os));
            fankui.write("上传成功");
            fankui.flush();
            socket.shutdownOutput();
        }

//        socket.close();
//        serverSocket.close();
    }
}
